import os

import requests
from bs4 import BeautifulSoup

res = requests.get(
    url="https://www.autohome.com.cn/news/",
    headers={
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0",
    }
)
res.encoding = 'gb2312'
soup = BeautifulSoup(res.text, features='html.parser')

info = soup.find(name='ul', attrs={'id': "tagInfo"})

li_list = info.find_all(name='li')
for li in li_list:
    div = li.find(name='div', attrs={"class": "editorname"})
    a = div.find(name='a')
    title = a.text

    img = li.find(name='img')
    url = "{}:{}".format("https", img.attrs['src'])
    print(title, url)

    res = requests.get(url=url)
    # print(res.content)

    file_name = "{}.jpg".format(title)
    file_path = os.path.join('images', file_name)

    with open(file_path, mode='wb') as f:
        f.write(res.content)